import { _decorator, Component, Node, Sprite, SpriteFrame } from 'cc';
import { ItemStatusEnum, ItemTypeEnum } from '../Enum';
import DataManger from '../RunTime/DataManager';
import { RendorManager } from '../Base/RendorManager';
const { ccclass, property } = _decorator;

@ccclass('ItemManager')
export class ItemManager extends RendorManager {
    label = "item"
    type: ItemTypeEnum

    @property(SpriteFrame)
    sceneSF: SpriteFrame = null;
    @property(SpriteFrame)
    inventorySF: SpriteFrame = null;

    /** 根据datamanager渲染物品 */
    rendor(): void {
        const item = DataManger.Instance.items.find(i => i.type === this.type)
        const spriteComp = this.getComponent(Sprite)
        
        switch (item.status) {
            case ItemStatusEnum.Scene:
                this.node.active = true
                spriteComp.spriteFrame = this.sceneSF
                break;
            case ItemStatusEnum.Inventory:
                this.node.active = true
                spriteComp.spriteFrame = this.inventorySF
                break;
            case ItemStatusEnum.Disable:
                this.node.active = false
                break;
            default: break
        }
    }
        
    start() {
        super.start()
        this.node.on(Node.EventType.TOUCH_END,this.touchEnd,this)
    }

    onDestroy() {
        super.onDestroy()
        this.node.off(Node.EventType.TOUCH_END, this.touchEnd,this)
    }

    touchEnd() {
        const item = DataManger.Instance.items.find(i => i.type === this.type)

        if (!item) {
            return
        }
        if (item.status === ItemStatusEnum.Scene) {
            item.status = ItemStatusEnum.Inventory
            DataManger.Instance.items = [...DataManger.Instance.items]
        }

        
    }

    
}


